#pragma warning(disable:4996)
#pragma warning(disable:6031)

#include <iostream>
#include <vector>
using namespace std;

void SelectSort(vector<int>& arr)
{
	for (int i = 0; i < arr.size(); i++)
	{
		int mini = i;
		for (int j = mini + 1; j < arr.size(); j++)
		{
			if (arr[j] < arr[mini])
			{
				mini = j;
			}
		}
		swap(arr[i], arr[mini]);
	}
}

void InsertSort(vector<int>& arr)
{
	for (int i = 0; i < arr.size() - 1; i++)
	{
		int end = i;
		int tmp = arr[end + 1];
		while (end >= 0)
		{
			if (arr[end] > tmp)
			{
				arr[end + 1] = arr[end];
				end--;
			}
			else
			{
				break;
			}
		}
		arr[end + 1] = tmp;
	}
}

void BubbleSort(vector<int>& arr)
{
	for (int i = 0; i < arr.size() - 1; i++)
	{
		bool flag = true;
		for (int j = 0; j < arr.size() - 1 - i; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				swap(arr[j], arr[j + 1]);
				flag = false;
			}
		}
		if (flag)
		{
			break;
		}
	}
}

void ShellSort(vector<int>& arr)
{
	int gap = arr.size();
	while (gap > 1)
	{
		gap = gap / 3 + 1;
		for (int i = 0; i < arr.size() - gap; i++)
		{
			int end = i;
			int tmp = arr[end + gap];
			while (end >= 0)
			{
				if (arr[end] > tmp)
				{
					arr[end + gap] = arr[end];
					end -= gap;
				}
				else
				{
					break;
				}
			}
			arr[end + gap] = tmp;
		}
	}
}

void AdjustUp(vector<int>& arr, int child)
{
	int parent = (child - 1) / 2;
	while (parent >= 0)
	{
		if (arr[child] > arr[parent])
		{
			swap(arr[child], arr[parent]);
			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
			break;
		}
	}
}
void AdjustDown(vector<int>& arr, int parent, int size)
{
	int child = parent * 2 + 1;
	while (child < size)
	{
		if (child + 1 < size && arr[child] < arr[child + 1])
		{
			child++;
		}
		if (arr[parent] < arr[child])
		{
			swap(arr[parent], arr[child]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}

void HeapSort(vector<int>& arr)
{
	for (int i = 0; i < arr.size(); i++)
	{
		AdjustUp(arr, i);
	}
	for (int i = arr.size() - 1; i >= 0; i--)
	{
		swap(arr[i], arr[0]);
		AdjustDown(arr, 0, i);
	}
}

void _MergeSort(vector<int>& arr, int left, int right, vector<int>* tmp)
{
	if (left >= right)
	{
		return;
	}
	int mid = (left + right) / 2;
	_MergeSort(arr, left, mid, tmp);
	_MergeSort(arr, mid + 1, right, tmp);
	int begin1 = left, end1 = mid;
	int begin2 = mid + 1, end2 = right;
	int index = left;
	while (begin1 <= end1 && begin2 <= end2)
	{
		if (arr[begin1] < arr[begin2])
		{
			(*tmp)[index++] = arr[begin1++];
		}
		else
		{
			(*tmp)[index++] = arr[begin2++];
		}
	}
	while (begin1 <= end1)
	{
		(*tmp)[index++] = arr[begin1++];
	}
	while (begin2 <= end2)
	{
		(*tmp)[index++] = arr[begin2++];
	}
	for (int i = left; i <= right; i++)
	{
		arr[i] = (*tmp)[i];
	}
}

void MergeSort(vector<int>& arr)
{
	auto* tmp = new vector<int>(arr.size());
	_MergeSort(arr, 0, arr.size() - 1, tmp);
	delete tmp;
}

void CountSort(vector<int>& arr)
{
	int max = arr[0];
	int min = arr[0];
	for (auto e : arr)
	{
		if (max < e)
		{
			max = e;
		}
		if (min > e)
		{
			min = e;
		}
	}
	int range = max - min + 1;
	auto* count = new vector<int>(range, 0);
	for (int i = 0; i < arr.size(); i++)
	{
		(*count)[arr[i] - min]++;
	}
	int index = 0;
	for (int i = 0; i < range; i++)
	{
		while ((*count)[i]--)
		{
			arr[index++] = i + min;
		}
	}
	delete count;
}


int main()
{
	static int a = 10;
	return 0;
}

//int main()
//{
//	vector<int> arr = { 5,1,1,4,8,7,0,2,3,3,6,1,9,0,1 };
//
//	//SelectSort(arr);
//	//InsertSort(arr);
//	//BubbleSort(arr);
//	//ShellSort(arr);
//	//HeapSort(arr);
//	//MergeSort(arr);
//	//CountSort(arr);
//
//	for (auto e : arr)
//	{
//		cout << e << ' ';
//	}
//	cout << endl;
//
//	return 0;
//}